const jsdom = require("jsdom");
const { JSDOM } = jsdom;

const crc32 = function (t) {
  for (var e, r = [], n = 0; n < 256; n++) {
    e = n;
    for (var o = 0; o < 8; o++)
      e = 1 & e ? 3988292384 ^ e >>> 1 : e >>> 1;
    r[n] = e
  }
  for (var i = -1, a = 0; a < t.length; a++)
    i = i >>> 8 ^ r[255 & (i ^ t.charCodeAt(a))];
  return (-1 ^ i) >>> 0
};
const genRandomString = function (t) {
  const r = Array(t).fill(undefined)
  return r.map(i => {
    return "abcdefghijklmnopqrstuvwxyz1234567890"[Math.floor(36 * Math.random())]
  }).join("")
}
const generateA1 = function () {
  const a = 3
  const o = "".concat((+new Date).toString(16))
  const n = o.concat(genRandomString(30))
  const r = n.concat(a)
  const e = r.concat("0")
  const u = e.concat("000")
  const c = crc32(u);
  const i = "".concat(u)
  return i.concat(c).substring(0, 52)
}
const a1 = generateA1()

const resourceLoader = new jsdom.ResourceLoader({
  userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
});

const xhs = 'https://www.xiaohongshu.com'

const options = {
  url: xhs,
  referrer: xhs,
  contentType: "text/html",
  resources: resourceLoader,
  pretendToBeVisual: true,
  cookieJar: new jsdom.CookieJar(),
}

const cookies = {
  sec_poison_id: '31a03e96-ce74-4c56-aeaf-9c678ff65b4f',
  web_session: '030037a289cffd0184c0c5ddc2224a34e5f9e9',
  abRequestId: 'bae9e981-4cc3-5851-8882-726ac2890ce9',
  webId: 'e25fd42c73c60f9495dde0ff1e882c8d',
  websectiga: '29098a4cf41f76ee3f8db19051aaa60c0fc7c5e305572fec762da32d457d76ae',
  gid: 'yYfK8dijJyAYyYfK8diYddUjJYEYuETMklUlAjqxIMTMWxq866uVCS888JJ8YKY8D84qSjd4',
  webBuild: '4.1.6',
  a1: a1,
  xsecappid: 'xhs-pc-web',
}

for (const key in cookies) {
  const str = key + '=' + cookies[key] + ';'
  options.cookieJar.setCookieSync(str, xhs)
}

const dom = new JSDOM(`<!DOCTYPE html>`, options);


exports.dom = dom
exports.a1 = a1